package com.itboye.eweilisten.me.timu;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.base.BaseActivity;
import com.itboye.eweilisten.model.YesCuoBean;
import com.itboye.eweilisten.util.loadingutil.MAlert;
import com.itboye.eweilisten.util.widget.PagerAdapter;

import java.util.List;

import static com.itboye.eweilisten.me.timu.ZongHeActivity.player;

/**
 * 错题分析
 */
public class CuoFenXiActivity extends BaseActivity {
    TextView zuoti_title, zuoti_title01;
    ViewPager viewpager;
    TextView jiexi_shang, jiexi_xia, tvTitle;
    String id;
    YesCuoBean bean;

    List<YesCuoBean.InfoList> list;

    FenXiFragment f1 = null;
    PagerAdapter pagerAdapter;
    int position = 0;
    ImageView back, img_yinyue, img_fanyi, img_ting;
    public static CuoFenXiActivity activity;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cuo_fen_xi);
        activity = this;
        tvTitle.setText("错题分析");
        id = getIntent().getStringExtra("id");
        bean = (YesCuoBean) getIntent().getSerializableExtra("bean");
        list = bean.getList();
        pagerAdapter = new PagerAdapter(getSupportFragmentManager());
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)) {
                position = i;
            }
            f1 = new FenXiFragment(list.get(i),bean);
            pagerAdapter.addFragment(f1);
        }
        viewpager.setAdapter(pagerAdapter);
        viewpager.setCurrentItem(position);
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                CuoFenXiActivity.this.position = position;
                xian(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        xian(position);
        if(bean.getList().get(position).getDt_type().equals("6227")){
            img_ting.setVisibility(View.GONE);
        }
    }

    /**
     * 上一题，下一题显示
     *
     * @param pon
     */
    private void xian(int pon) {
        if (pon == 0 && pon != list.size() - 1) {
            jiexi_shang.setBackgroundColor(getResources().getColor(R.color.conv_list_split_line_color));
            jiexi_shang.setClickable(false);
            jiexi_xia.setBackgroundColor(getResources().getColor(R.color.green_01));
            jiexi_xia.setClickable(true);
        } else if (pon != 0 && pon == list.size() - 1) {
            jiexi_shang.setBackgroundColor(getResources().getColor(R.color.red_01));
            jiexi_shang.setClickable(true);
            jiexi_xia.setBackgroundColor(getResources().getColor(R.color.conv_list_split_line_color));
            jiexi_xia.setClickable(false);
        } else if (pon == 0 && pon == list.size() - 1) {
            jiexi_shang.setBackgroundColor(getResources().getColor(R.color.conv_list_split_line_color));
            jiexi_shang.setClickable(false);
            jiexi_xia.setBackgroundColor(getResources().getColor(R.color.conv_list_split_line_color));
            jiexi_xia.setClickable(false);
        } else {
            jiexi_shang.setBackgroundColor(getResources().getColor(R.color.red_01));
            jiexi_shang.setClickable(true);
            jiexi_xia.setBackgroundColor(getResources().getColor(R.color.green_01));
            jiexi_xia.setClickable(true);
        }
    }

    public void setCurrentPager(int cu) {
        viewpager.setCurrentItem(cu);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);

        switch (view.getId()) {

            case R.id.jiexi_shang://上一题
                if (player!=null) {
                    if (player.isPlaying()) {
                        if (player.isPlaying()) {
                            player.stop();
                        }
                    }
                }
                position--;
                activity.setCurrentPager(position);
                xian(position);
                break;
            case R.id.jiexi_xia://下一题
                if (player != null) {
                    if (player.isPlaying()) {
                        player.stop();
                    }
                }
                position++;
                activity.setCurrentPager(position);
                xian(position);
                break;
            case R.id.back:
                finish();
                break;
            case R.id.img_yinyue:
                //显示题目详情
                ((FenXiFragment) pagerAdapter.getItem(position)).beanTi.get(0).setShowAnswerTrue(!((FenXiFragment) pagerAdapter.getItem(position)).beanTi.get(0).isShowAnswerTrue());
                ((FenXiFragment) pagerAdapter.getItem(position)).setAdater();
                break;
            case R.id.img_fanyi:
                String msg = ((FenXiFragment) pagerAdapter.getItem(position)).beanTi.get(0).getOriginArticle();
                showPopupWindow(msg);
                break;
            case R.id.img_ting:

                if(((FenXiFragment) pagerAdapter.getItem(position)).beanTi.get(0).getAudioPath()!=null&&
                        !"".equals(((FenXiFragment) pagerAdapter.getItem(position)).beanTi.get(0).getAudioPath())){

                    if (view.getTag(R.integer.position)!=null) {
                        if (view.getTag(R.integer.position).equals(position + "")) {
                            if (player.isPlaying()) {
                                player.stop();
                                MAlert.alert("停止播放");
                            } else {
                                player = MediaPlayer.create(this, Uri.parse(((FenXiFragment) pagerAdapter.getItem(position)).beanTi.get(0).getAudioPath()));
                                player.start();
//                                view.setTag(R.integer.position,position);
                            }
                            break;
                        }
                    }
                    player = MediaPlayer.create(this, Uri.parse(((FenXiFragment) pagerAdapter.getItem(position)).beanTi.get(0).getAudioPath()));

                    if (player.isPlaying()) {
                        player.stop();
                        MAlert.alert("停止播放");
                    } else {
                        player.start();
                        view.setTag(R.integer.position,""+position);
                    }
                }else {
                    MAlert.alert("没有音频文件");
                }

                break;
        }


    }

    @Override
    public void finish() {
        super.finish();
        if (player.isPlaying()){
            player.stop();
            MAlert.alert("已停止播放音频");
        }
    }

    private void showPopupWindow(String msg) {
        final View contentView = LayoutInflater.from(this).inflate(
                R.layout.popwindow_tips, null);
        final PopupWindow popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setAnimationStyle(R.style.popupwindow_anim_style);
        TextView txt_tips;
        contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        txt_tips = (TextView) contentView.findViewById(R.id.txt_tips);
        String[] split = msg.split("\\(");
        StringBuffer mStringBuffer=new StringBuffer();
        for (int i=0;i<split.length;i++){
            if(i==0){
                mStringBuffer.append(split[i]+"<br>");
            }else {
                mStringBuffer.append("("+split[i]+"<br>");
            }

        }
        txt_tips.setText(Html.fromHtml(mStringBuffer.toString()));
        popupWindow.setTouchable(true);
        // 设置好参数之后再show
        popupWindow.showAtLocation(contentView, Gravity.BOTTOM, 0, 0);
    }


}
